package com.moqianfan.mcuassistant.activity;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.utils.FormatUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by limh on 2016/11/21.
 * 波特率计算
 */

public class BpsCalculActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.spin_sm)
    Spinner SpinnSM;
    @BindView(R.id.spin_ocxo)
    Spinner SpinnOcox;
    @BindView(R.id.radioG_sm)
    RadioGroup radioSmod;
    @BindView(R.id.edit_bps)
    EditText txtBps;
    @BindView(R.id.web_bps_info)
    WebView webBpsInfo;
    @BindView(R.id.line_sm13)
    LinearLayout lineSmod;
    @BindView(R.id.txt_thl)
    TextView txtThl;
    private String[] smItem = {"方式0", "方式1", "方式2", "方式3"};
    private String[] ocoxItem = {"4", "6", "8", "11.0592",
            "12", "14", "16", "18.432", "20", "22.1184"};

    private int smod = 0;
    private String ocox = "11.0592";
    private String sm = "方式1";

    @Override
    public void findViews() {
        setContentView(R.layout.activity_bps);
    }

    @SuppressLint("SetJavaScriptEnabled")
    public void initViews() {
        ArrayAdapter<String> smAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, smItem);
        smAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SpinnSM.setAdapter(smAdapter);
        SpinnSM.setSelection(1, true);
        SpinnSM.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sm = smItem[i];
                txtThl.setText("");
                txtBps.setText("");
                getResult();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        final ArrayAdapter<String> ocoxAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, ocoxItem);
        ocoxAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SpinnOcox.setAdapter(ocoxAdapter);
        SpinnOcox.setSelection(3, true);
        SpinnOcox.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                ocox = ocoxItem[i];
                txtThl.setText("");
                txtBps.setText("");
                getResult();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        radioSmod.setOnCheckedChangeListener(this);
        webBpsInfo.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webBpsInfo.getSettings().setLoadWithOverviewMode(true);
        webBpsInfo.getSettings().setJavaScriptEnabled(true);
        webBpsInfo.loadUrl("file:///android_asset/html/mcu_bps.html");
    }

    @Override
    public void initDatas() {

    }

    private void getResult() {

        switch (sm) {
            case "方式0":
                txtBps.setEnabled(false);
                Double bps = (Double.valueOf(ocox) * 1000000) / 12;
                lineSmod.setVisibility(View.GONE);
                txtBps.setText(FormatUtil.formatBps(bps));
                break;
            case "方式1":
                txtBps.setEnabled(true);
                lineSmod.setVisibility(View.VISIBLE);
                Double x;
                if (!TextUtils.isEmpty(txtBps.getText().toString())) {
                    x = 256 - ((Double.valueOf(ocox) * 1000000) * (smod + 1)) / (384 * Double.parseDouble(txtBps.getText().toString()));
                    if (Integer.parseInt(FormatUtil.formatBps(x)) > 255) {
                        Toast.makeText(this, "波特率太大", Toast.LENGTH_SHORT).show();
                    } else {
                        txtThl.setText(FormatUtil.formatBps(x) + "=" + Integer.toHexString(Integer.parseInt(FormatUtil.formatBps(x))).toUpperCase() + "H");
                    }
                }
                break;
            case "方式2":
                txtBps.setEnabled(false);
                bps = Math.pow(2, smod) / 64 * (Double.valueOf(ocox) * 1000000);
                txtBps.setText(FormatUtil.formatBps(bps));
                lineSmod.setVisibility(View.VISIBLE);
                break;
            case "方式3":
                txtBps.setEnabled(true);
                lineSmod.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(txtBps.getText().toString())) {
                    x = 256 - ((Double.valueOf(ocox) * 1000000) * (smod + 1)) / (384 * Double.parseDouble(txtBps.getText().toString()));
                    if (Integer.parseInt(FormatUtil.formatBps(x)) > 255) {
                        Toast.makeText(this, "波特率太大", Toast.LENGTH_SHORT).show();
                    } else {
                        txtThl.setText(FormatUtil.formatBps(x) + "=" + Integer.toHexString(Integer.parseInt(FormatUtil.formatBps(x))).toUpperCase() + "H");
                    }
                }
                break;
        }

    }

    @OnClick({R.id.image_close, R.id.btn_calcul})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
            case R.id.btn_calcul:
                if (!TextUtils.isEmpty(txtBps.getText().toString()))
                    getResult();
                else {
                    Toast.makeText(this, "请输入波特率", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.radio_0:
                smod = 0;
                break;
            case R.id.radio_1:
                smod = 1;
                break;
        }
        getResult();
    }
}
